      // index.js
      // 获取应用实例
      const app = getApp()
      const ipconfig = app.globalData.ipconfig;
      var http = require('../../utils/http')
      var utils = require('../../utils/util')
      Page({
          /**
           * 页面的初始数据
           */
          data: {
              loginStatus:false,
              hasToken:false,
              userInfo: {
                avatar: 'http://ruuqwf8y6.hn-bkt.clouddn.com/%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F.jpg',
                nickName: "未登录",
                phoneNumber:''
              },
              temp: {
                avatar: 'http://ruuqwf8y6.hn-bkt.clouddn.com/%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F.jpg',
                nickName: "未登录",
                phoneNumber:''
              },
              menuList: [
                  {
                      'id': "2",
                      "avatar": "http://ruuqwf8y6.hn-bkt.clouddn.com/%E6%88%91%E7%9A%84%E6%94%B6%E8%97%8F.jpg",
                      "userName": "我的收藏",
                      "path": "/pages/user/myfavourite/myfavourite"
                  },
                  {
                      'id': "3",
                      "avatar": "http://ruuqwf8y6.hn-bkt.clouddn.com/%E6%B5%8F%E8%A7%88%E8%AE%B0%E5%BD%95.jpg",
                      "userName": "浏览记录",
                      "path": "/pages/user/mylook/mylook"
                  }
              ],
              feedbackList: [
                  {
                      'id': "1",
                      "avatar": "http://ruuqwf8y6.hn-bkt.clouddn.com/%E7%94%A8%E6%88%B7%E5%8F%8D%E9%A6%88.jpg",
                      "feedbackName": "用户反馈",
                      "path": "/pages/user/feedback/feedback"
                  },
                  {
                      'id': "2",
                      "avatar": "http://ruuqwf8y6.hn-bkt.clouddn.com/%E8%AF%BE%E7%A8%8B%E5%BB%BA%E8%AE%AE.jpg",
                      "feedbackName": "课程建议",
                      "path": "/pages/user/suggest/suggest"
                  },
                  {
                      'id': "3",
                      "avatar": "http://ruuqwf8y6.hn-bkt.clouddn.com/%E9%82%80%E8%AF%B7%E5%A5%BD%E5%8F%8B.jpg",
                      "feedbackName": "邀请好友",
                      "path": "/pages/user/invite/invite"
                  },
                  {
                      'id': "4",
                      "avatar": "http://ruuqwf8y6.hn-bkt.clouddn.com/%E8%81%94%E7%B3%BB%E6%88%91%E4%BB%AC.jpg",
                      "feedbackName": "联系我们",
                      "path": "/pages/user/connect/connect"
                  }
              ]
          },
          getUserInfo() {
              http.get("/mini/testToken","",res=>{
                  if(res.statusCode == 401){
                      this.setData({
                        loginStatus:false
                      })
                  }else{
                    this.setData({
                        loginStatus:true
                      })
                  }
                  if(this.data.loginStatus){
                    
                      this.setData({
                          'userInfo.avatar': wx.getStorageSync("userInfo").avatar,
                          'userInfo.nickName':wx.getStorageSync("userInfo").nickName,
                          'userInfo.phoneNumber':wx.getStorageSync("userInfo").phoneNumber
                      })
                      
                  }
              })
          },
          logout() {
              wx.clearStorageSync();
              this.setData({
                userInfo:this.data.temp
            })
              this.getUserInfo()
              
          },

          toCommon(e) {
              const path = e.target.dataset.path;
              console.log(path);
              if(this.data.loginStatus){
                wx.navigateTo({
                    url: `${path}`,
                })
              }else{
                wx.navigateTo({
                    url: '/pages/login1/login1'
                })
              }
          },
           //登录
	login() {
		let that = this;
        // 登录
		wx.login({
			success(res1) {
				if (res1.code) {
                    //发起网络请求
                    wx.request({
                      url: 'http://'+ ipconfig + ":8080/mini/login",
                      method:"GET",
                      data:{code:res1.code},
                      success(res){
                        const {flag,data,message} = res.data;
                        console.log("flag,data,message",res);
                        const msg = message.split("::")
                        if (!flag) {
                            wx.showToast({
                                title: msg[0],
                                icon: 'fail',
                                duration: 2000
                            })
                        }
                        if(res.statusCode == 400){
                            that.register(msg[1],that)
                        }else{
                            //将登录信息存到本地
                            wx.setStorageSync('token', `${data.tokenHead} ${data.token}`);
                            wx.setStorageSync('userInfo', data.userInfo);
                            wx.setStorageSync('openId', data.openid)
                            that.setData({
                                'loginStatus':utils.hasToken()
                            })
                            //判断是否需要完善信息
                            if (data.userInfo.password == null) {
                                wx.navigateTo({
                                    url: '/pages/login/login',
                                })
                            }
                            that.getUserInfo()
                        }
                        
                      },
                      fail(err){
                        console.log('请求失败',err)
                      }
                    })
					
				} else {
					console.log('登录失败！' + res.errMsg)
				}
            },
            fail:function(res){
                console.log("请求失败",res);
            }
		});
    },

    register(openId,that){
        wx.request({
            url: 'http://' + ipconfig + ':8080/mini/register',
            method: 'GET',
            data: {openId:openId},
            success: function (res) {
                that.login()
            },
            fail: function (err) {
                console.log("请求失败",err);
            }
        })
    },

          onLoad: function (options) {
              this.getUserInfo()
          },


          onShow: function () {
              this.getTabBar().init();
              this.getUserInfo()
          },

          
      })